package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.SelectAdapter;
import zj.com.adapter.ZyAdapter;
import zj.com.db.DBUtil;
import zj.com.model.Zy;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/17.
 *
 * 专业
 */
public class ZyActivity extends  BaseActivity implements View.OnClickListener{


    public ListView listView;
    public TextView title_name;
    public ImageView left_iv;
    public int type;
    public ZyAdapter adapter;
    public List<Zy> list;
    public ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zy);
        initView();
        initData();
    }

    public void initView(){
        list = new ArrayList<Zy>();

        listView = (ListView) findViewById(R.id.listView);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("专业选择");


        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
    }

    public void initData(){


        adapter = new ZyAdapter(list,this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                LogUtils.getInstance().error(list.get(position).getSpecialtyName());
                Intent intent =new Intent();
                intent.putExtra("zy", list.get(position));
                setResult(RESULT_OK, intent);
                finish();

            }
        });

        try {

          list =   DBUtil.getInstance(this).getDBManager().selector(Zy.class).findAll();
          if(list == null){
              obtainZy();
          }else{
            adapter.setDataChange(list);
          }

        }catch(Exception e){

        }

    }

    public void obtainZy(){


        dialog = DialogUtil.createProgressDialog(this,"获取数据","数据获取中..");
        NetManager.getInstance(this).obtainZy(new OnResponseListener<List<Zy>>() {
            @Override
            public void onComplete(boolean isOK, List<Zy> result, int errorCode, String errorMsg) {

                if(isOK){

                    list = result;
                    adapter.setDataChange(result);
                }else{
                    Toast.makeText(ZyActivity.this,"获取失败",Toast.LENGTH_SHORT).show();

                }
                dialog.dismiss();


            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
                Toast.makeText(ZyActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
        }
    }
}
